package uy.com.nfc.localassigner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import uy.com.nfc.localassigner.adapters.PointsListAdapter;
import uy.com.nfc.localassigner.managers.SessionManager;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.example.nfctagcommons.classes.VigilancePoint;
import com.example.nfctagcommons.dialog.LoadOnBackgroundDialog;
import com.example.nfctagcommons.dialog.LoadOnBackgroundDialog.IBackgroundAction;
import com.example.nfctagcommons.server.ServerConnector;
import com.example.nfctagcommons.utils.SystemConstants;

public class MyPoints extends Activity {
	private static final int[] textViewIds = {
		R.id.backActionBar1
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_locals);
		setCustomTypeface();
		setTitle(R.string.title_activity_my_locals);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//		findViewById(R.id.workingOfflineLayout).setVisibility(View.GONE);
		if (!SessionManager.isLoggedIn(this)){
			startActivity(new Intent(getApplicationContext(), MainActivity.class));
			finish();
			return;
		}
		
		LoadOnBackgroundDialog background = new LoadOnBackgroundDialog(this);
		background.load(true, "Obteniendo lista de puntos", new IBackgroundAction() {
			private List<VigilancePoint> pointsList;
			@Override
			public boolean doBackgroundAction() {
				try {
					String guardId = SessionManager.getGuardId(MyPoints.this);
					pointsList = ServerConnector.getVigilanceSetAsList(MyPoints.this, guardId);
					return true;
				} catch (Exception e) {
					e.printStackTrace(); 
					return false;
				}
			}
			@Override
			public void doPostExecuteAction(boolean success) {
				if (success){
					ListView localListView = (ListView)findViewById(R.id.myLocals);
					String[] items = new String[pointsList.size()];
					localListView.setAdapter(new PointsListAdapter(MyPoints.this,items,pointsList));
				}else{
					try{
						HashMap<String, VigilancePoint> localHashMap = SessionManager.getMyAssignedPoints(MyPoints.this);
						pointsList = new ArrayList<VigilancePoint>();
						for (VigilancePoint local : localHashMap.values()){
							pointsList.add(local);
						}
						String[] items = new String[pointsList.size()];
						ListView localListView = (ListView)findViewById(R.id.myLocals);
						localListView.setAdapter(new PointsListAdapter(MyPoints.this,items,pointsList));
//						findViewById(R.id.workingOfflineLayout).setVisibility(View.VISIBLE);
					}catch (Exception x){
						x.printStackTrace();
						startActivity(new Intent(getApplicationContext(), MainActivity.class));
						finish();
					}
				}
			}
			@Override
			public void onCancelled() {
				startActivity(new Intent(getApplicationContext(), MainActivity.class));
				finish();
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		SystemConstants.syncTime(this);
	}
	public void back(View view){
		onBackPressed();
	}
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(getApplicationContext(), MainActivity.class);
		intent.putExtra(MainActivity.FROM_FROWARD_SCREEN, true);
		startActivity(intent);
		finish();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case android.R.id.home:
		    	onBackPressed();
		        return true;
	    }
	    return super.onOptionsItemSelected(item);
	}
	private void setCustomTypeface() {
		Typeface customTypeface = Typeface.createFromAsset(getAssets(), "fonts/AliquamREG.ttf");
		for (int id : textViewIds){
			((TextView)findViewById(id)).setTypeface(customTypeface);
		}
	}
}
